
//
//  PhotoBrowserVC.swift
//  SwiftPhophotoBrowser
//
//  Created by xmg on 16/8/20.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit

private let reuseIdentifier = "photoCell"

class PhotoBrowserVC: UIViewController {

    /** 懒加载属性UICollectionView */
    private var collectionView : UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: PhotoBrowserViewLayout())
    /** 设置商品属性 */
    var shops : [CommodityDisplay]?
    var indexPath : NSIndexPath?
    
    override func viewDidLoad() {
        super.viewDidLoad()

       /** 控制器的View的宽度 */
       view.frame.size.width += 15
        
        // Register cell classes
        collectionView.frame = view.bounds
        collectionView.registerClass(PhotoBrowserViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        collectionView.dataSource = self
        collectionView.delegate = self
        view.addSubview(collectionView)
        /** 设置按钮 */
        setUpBtn()
        /** 滚动到正确的位置 */
        collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: .CenteredHorizontally, animated: false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

  

}
extension PhotoBrowserVC {

    func setUpBtn(){
    
        /** 创建按钮 */
        let closeBtn = UIButton(title: "关 闭", bgColor: UIColor.lightGrayColor(), fontSize: 14.0)
        let saveBtn = UIButton(title: "保 存", bgColor: UIColor.lightGrayColor(), fontSize: 14.0)
        /** 添加按钮 */
        view.addSubview(closeBtn)
        view.addSubview(saveBtn)
        /** 设置圆角 */
        closeBtn.layer.cornerRadius = 10
        saveBtn.layer.cornerRadius = 10
        /** 设置尺寸 */
        let btnW :CGFloat = 100
        let btnH : CGFloat = 32
        let margin : CGFloat = 20
        var btnX = margin
        let btnY = UIScreen.mainScreen().bounds.height - btnH - margin
        closeBtn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
        btnX = UIScreen.mainScreen().bounds.width - btnW - margin
        saveBtn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
        
        /** 监听按钮点击 */
        closeBtn.addTarget(self, action: "closeBtnClick", forControlEvents: .TouchUpInside)
        saveBtn.addTarget(self, action: "saveBtnClick", forControlEvents: .TouchUpInside)
    }
}

extension PhotoBrowserVC {
 
    /** 关闭 */
    func closeBtnClick() {
    dismissViewControllerAnimated(true, completion: nil)
    }
    /** 保存图片 */
    func saveBtnClick(){
        let cell = collectionView.visibleCells()[0] as! PhotoBrowserViewCell
        guard let image = cell.imageView.image else{return}
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
    }
}

extension PhotoBrowserVC : UICollectionViewDataSource {
    // MARK: UICollectionViewDataSource
    @objc internal func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return shops?.count ?? 0
    }
    
    @objc internal func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoBrowserViewCell
        
        cell.shops = shops! [indexPath.item]
        
        return cell
    }
    
}
extension PhotoBrowserVC : UICollectionViewDelegate{
    /** 关闭 */
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        closeBtnClick()
    }
}

extension PhotoBrowserVC : AnimatorDismissedDelegate{

    func getCurrentIndexPath() -> NSIndexPath{
    
        /** 获取屏幕中的显示的cell */
        let cell = collectionView.visibleCells()[0]
        /** 获取cell对应的indexPath */
        let indexPath = collectionView.indexPathForCell(cell)
        return indexPath!
    }
    func getCurrentImageView() -> UIImageView{
    
        /** 创建UIImageView */
        let imageView = UIImageView()
        /** 设置imageView属性 */
        let cell = collectionView.visibleCells()[0] as! PhotoBrowserViewCell
        
        imageView.image = cell.imageView.image
        imageView.frame = calculatePictureSizeWithImage(imageView.image!)
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }
}